## This code replicates Figures 3, A14, A15, A16, A17

## setwd("/Users/sparshasaha/Dropbox/Seeking More Why Women Fail/Pol Behavior Rep Files Ambitious Women 02282020")
## setwd("C:/Users/acw64/Dropbox/Seeking More Why Women Fail/Pol Behavior Rep Files Ambitious Women 02282020")

##install.packages("devtools")
##library(devtools)
##devtools::install_github("jaredlander/coefplot")

library(dplyr)
library(tidyverse)
library(coefplot)

################################### SSI Survey ###################################
#######################################################################################

## FIGURE 3

# Replication Archive for: 
# Kirkland, Patricia A. and Alexander Coppock
# Candidate Choice without Party Labels: New Insights from Conjoint Survey Experiments
# Forthcoming at Political Behavior
# Helper Functions

#### Adapted for SSI data

gen_entry <- function(est, se, p){
  entry <- paste0(format_num(est, digits = 2), " (", format_num(se, 2), ")")
  
  if(p < 0.05) {
    entry <- paste0(entry, "*")
  }
  return(entry)
}

gen_entry_vec <- Vectorize(gen_entry)

se_mean <- function(x){
  x_nona <- x[!is.na(x)]
  n <- length(x_nona)
  return(sd(x_nona)/(sqrt(n)))
}

format_num <- function(x, digits=3){
  x <- as.numeric(x)
  return(paste0(sprintf(paste0("%.", digits, "f"), x)))
}

make_attributes2 <- function(df){
  df <- within(df,{
               attribute = factor(attribute, 
                                  levels = c("Yes", "Base category: No", 
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator",
                                             "Hard-Working", 
                                             "Tough Negotiator", 
                                             "Base category: Empathetic", 
                                             "3 children",
                                             
                                             "2 children", 
                                             "1 child", 
                                             "Base category: No children",
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
               })
  return(df)
}

make_coef_group2 <- function(df){
  df <- within(df,{
    coef_group <- rep(NA, nrow(df))
    coef_group[grepl(pattern="Progressive", x = rowname)] <- "Progressive"
    coef_group[grepl(pattern="Personalistic", x = rowname)] <- "Personalistic"
    coef_group[grepl(pattern="Parental", x = rowname)] <- "Parental"
    coef_group[grepl(pattern="Agenda", x = rowname)] <- "Agenda"
    coef_group <- factor(coef_group, levels=c("Progressive", "Personalistic", "Parental", 
                                              "Agenda"))  
  })
  return(df)
}


prep_for_gg2 <- function(fit_cl, WP=FALSE){
  df <- data.frame(ests =fit_cl[,1], ses = fit_cl[,2]) %>%
    add_rownames()
  df <- filter(df, grepl(pattern = "Progressive|Personalistic|Parental|Agenda", x = df$rowname))
  df <- within(df,{
    uis <- ests + 1.96*ses
    lis <- ests - 1.96*ses
    attribute <- sub(pattern = "Progressive|Personalistic|Parental|Agenda", replacement = "", x = df$rowname)
    attribute <- factor(attribute, levels = c("Yes", "Base category: No", 
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator",
                                             "Hard-Working", 
                                             "Tough Negotiator", 
                                             "Base category: Empathetic", 
                                             "3 children",
                                             
                                             "2 children", 
                                             "1 child", 
                                             "Base category: No children",
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
    if(WP){
      attribute <- sub(pattern = "Progressive|Personalistic|Parental|Agenda", replacement = "", x = df$rowname)
      attribute <- factor(attribute, levels = c("Yes", "Base category: No", 
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator",
                                             "Hard-Working", 
                                             "Tough Negotiator", 
                                             "Base category: Empathetic", 
                                             "3 children",
                                             
                                             "2 children", 
                                             "1 child", 
                                             "Base category: No children",
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
    }
  })
  return(df)
}


cl   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) }

cl_vcov   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  return(vcovCL)}

type_base_categories_ssi = read.csv("type_base_categories_ssi.csv", stringsAsFactors=F)

## feed in data

mydata2 = read.csv("ssi.csv")
dim(mydata2)

newdata <- mydata2

## For Robustness Check, First Election Subset (Figure A20), simply uncomment what is below and run that instead of newdata <- mydata2 (above)
## newdata <- subset(mydata2, election == 1)

## relevel to specified baselines

newdata$Personalistic  <- relevel(newdata$Personalistic, "Empathetic")
newdata$Parental  <- relevel(newdata$Parental, "No children")
newdata$Agenda  <- relevel(newdata$Agenda, "Very Few Changes")

## code below computes and plots AMCEs

fit_1 <-
  lm(
    candidate_vote ~ Progressive + Personalistic + Parental + Agenda,
    data = filter(newdata, Gender == "Male")
  )
fit_2 <-
  lm(
    candidate_vote ~ Progressive + Personalistic + Parental + Agenda,
    data = filter(newdata, Gender == "Female")
  )
fit_3 <-
  lm(
    candidate_vote ~ (Progressive + Personalistic + Parental + Agenda) *  (Gender == "Female") ,
    data = newdata
  )


fit_1_cl <- cl(
  dat = filter(newdata, Gender == "Male"),
  cluster = filter(newdata, Gender == "Male")$id,
  fm = fit_1
)
fit_2_cl <- cl(
  dat = filter(newdata, Gender == "Female"),
  cluster = filter(newdata, Gender == "Female")$id,
  fm = fit_2
)
fit_3_cl <- cl(dat = newdata,
               cluster = newdata$id,
               fm = fit_3)

fit_1_df <-
  data.frame(fit_1_cl[, ], group = "Male") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")
fit_2_df <-
  data.frame(fit_2_cl[, ], group = "Female") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")
fit_3_df <-
  data.frame(fit_3_cl[, ], group = "Difference") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")

## Keep only interactions

fit_3_df <- fit_3_df[14:25, ]
fit_3_df$rowname <- fit_1_df$rowname
fit_3_df


all_df <- rbind(fit_1_df, fit_2_df, fit_3_df) %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    uis = Estimate + 1.96 * Std..Error,
    attribute = sub(
      pattern = "Progressive|Personalistic|Parental|Agenda",
      replacement = "",
      x = rowname
    )
  ) %>%
  bind_rows(type_base_categories_ssi) %>%
  make_attributes2() %>%
  make_coef_group2()


table_d4 <- 
all_df %>%
  filter(!is.na(Estimate)) %>%
  mutate(entry = gen_entry_vec(est = Estimate, se = Std..Error, p = Pr...t..))  %>%
  select(coef_group, attribute, group, entry) %>%
  spread(key = group, value = entry) %>%
  select(-coef_group)

all_df$group<- factor(all_df$group, levels = c("Male", "Female", "Difference"))


g3 <-
  ggplot(all_df, aes(x = Estimate, y = attribute)) +
  geom_point() +
  geom_segment(aes(yend = attribute, x = lis, xend = uis)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  facet_grid(coef_group ~ group, scales = "free_y") +
  coord_cartesian(xlim = c(-.5, .5)) +
  scale_x_continuous(breaks = round(seq(-.5, .5, .1), 1)) +
  theme_bw() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    strip.background = element_blank()
  )

g3

################################### DLABSS 1 Survey ###################################
#######################################################################################

## May be a good idea to start a new session to run code below because of similar functions from earlier

## FIGURE A15

# Replication Archive for: 
# Kirkland, Patricia A. and Alexander Coppock
# Candidate Choice without Party Labels: New Insights from Conjoint Survey Experiments
# Forthcoming at Political Behavior
# Helper Functions

#### Adapted for DLABSS 1 data

gen_entry <- function(est, se, p){
  entry <- paste0(format_num(est, digits = 2), " (", format_num(se, 2), ")")
  
  if(p < 0.05) {
    entry <- paste0(entry, "*")
  }
  return(entry)
}

gen_entry_vec <- Vectorize(gen_entry)

se_mean <- function(x){
  x_nona <- x[!is.na(x)]
  n <- length(x_nona)
  return(sd(x_nona)/(sqrt(n)))
}

format_num <- function(x, digits=3){
  x <- as.numeric(x)
  return(paste0(sprintf(paste0("%.", digits, "f"), x)))
}

make_attributes2 <- function(df){
  df <- within(df,{
               attribute = factor(attribute, 
                                  levels = c("Yes", "Base category: No", 
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator", "Hard-Working", "Tough Negotiator", 
                                             "Base category: Empathetic",
                                             
                                             "More than 20", 
                                             "10 - 20", 
                                             "5 - 10", 
                                             "Base category: 0 - 5",
                                             
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
               })
  return(df)
}

make_coef_group2 <- function(df){
  df <- within(df,{
    coef_group <- rep(NA, nrow(df))
    coef_group[grepl(pattern="Progressive", x = rowname)] <- "Progressive"
    coef_group[grepl(pattern="Personalistic", x = rowname)] <- "Personalistic"
    coef_group[grepl(pattern="Experience", x = rowname)] <- "Experience"
    coef_group[grepl(pattern="Agenda", x = rowname)] <- "Agenda"
    coef_group <- factor(coef_group, levels=c("Progressive", "Personalistic", "Experience", 
                                              "Agenda"))  
  })
  return(df)
}


prep_for_gg2 <- function(fit_cl, WP=FALSE){
  df <- data.frame(ests =fit_cl[,1], ses = fit_cl[,2]) %>%
    add_rownames()
  df <- filter(df, grepl(pattern = "Progressive|Personalistic|Experience|Agenda", x = df$rowname))
  df <- within(df,{
    uis <- ests + 1.96*ses
    lis <- ests - 1.96*ses
    attribute <- sub(pattern = "Progressive|Personalistic|Experience|Agenda", replacement = "", x = df$rowname)
    attribute <- factor(attribute, levels = c("Yes", "Base category: No", 
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator", "Hard-Working", "Tough Negotiator", 
                                             "Base category: Empathetic",
                                             
                                             "More than 20", 
                                             "10 - 20", 
                                             "5 - 10", 
                                             "Base category: 0 - 5",
                                             
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
    if(WP){
      attribute <- sub(pattern = "Progressive|Personalistic|Experience|Agenda", replacement = "", x = df$rowname)
      attribute <- factor(attribute, levels = c("Yes", "Base category: No", 
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator", "Hard-Working", "Tough Negotiator", 
                                             "Base category: Empathetic",
                                             
                                             "More than 20", 
                                             "10 - 20", 
                                             "5 - 10", 
                                             "Base category: 0 - 5",
                                             
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
    }
  })
  return(df)
}


cl   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) }

cl_vcov   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  return(vcovCL)}

type_base_categories_dlabss_one = read.csv("type_base_categories_dlabss_one.csv", stringsAsFactors=F)

## feed in data

mydata2 = read.csv("dlabss_one.csv")
dim(mydata2)

newdata <- mydata2

## clean up Experience because Excel changed numbers to dates

newdata$Experience1 <- as.numeric(newdata$Experience)

## More than 20 = 5 // 10 - 20 =  2 // 5 - 10 = 1 // 5 - 10 = 3 // 0 - 5 = 4

newdata$Experience2[newdata$Experience1 == 5] <- "More than 20"
newdata$Experience2[newdata$Experience1 == 4] <- "0 - 5"
newdata$Experience2[newdata$Experience1 == 3] <- "5 - 10"
newdata$Experience2[newdata$Experience1 == 2] <- "10 - 20"
newdata$Experience2[newdata$Experience1 == 1] <- "5 - 10"

newdata$Experience2 <- as.factor(newdata$Experience2)

## fix levels so they are linear

newdata$Experience2 <- factor(newdata$Experience2, levels = c("0 - 5", "5 - 10", "10 - 20", "More than 20"))

newdata$Experience <- NULL
newdata$Experience <- newdata$Experience2

## relevel to specified baselines

newdata$Personalistic  <- relevel(newdata$Personalistic, "Empathetic")
newdata$Experience  <- relevel(newdata$Experience, "0 - 5")
newdata$Agenda  <- relevel(newdata$Agenda, "Very Few Changes")

## code below computes and plots AMCEs

fit_1 <-
  lm(
    candidate_vote ~ Progressive + Personalistic + Experience + Agenda,
    data = filter(newdata, Gender == "Male")
  )
fit_2 <-
  lm(
    candidate_vote ~ Progressive + Personalistic + Experience + Agenda,
    data = filter(newdata, Gender == "Female")
  )
fit_3 <-
  lm(
    candidate_vote ~ (Progressive + Personalistic + Experience + Agenda) * (Gender == "Female"),
    data = newdata
  )


fit_1_cl <- cl(
  dat = filter(newdata, Gender == "Male"),
  cluster = filter(newdata, Gender == "Male")$id,
  fm = fit_1
)
fit_2_cl <- cl(
  dat = filter(newdata, Gender == "Female"),
  cluster = filter(newdata, Gender == "Female")$id,
  fm = fit_2
)
fit_3_cl <- cl(dat = newdata,
               cluster = newdata$id,
               fm = fit_3)

fit_1_df <-
  data.frame(fit_1_cl[, ], group = "Male") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")
fit_2_df <-
  data.frame(fit_2_cl[, ], group = "Female") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")
fit_3_df <-
  data.frame(fit_3_cl[, ], group = "Difference") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")

## Keep only interactions

fit_3_df <- fit_3_df[14:25, ]
fit_3_df$rowname <- fit_1_df$rowname


all_df <- rbind(fit_1_df, fit_2_df, fit_3_df) %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    uis = Estimate + 1.96 * Std..Error,
    attribute = sub(
      pattern = "Progressive|Personalistic|Experience|Agenda",
      replacement = "",
      x = rowname
    )
  ) %>%
  bind_rows(type_base_categories_dlabss_one) %>%
  make_attributes2() %>%
  make_coef_group2()

table_d4 <- 
all_df %>%
  filter(!is.na(Estimate)) %>%
  mutate(entry = gen_entry_vec(est = Estimate, se = Std..Error, p = Pr...t..))  %>%
  select(coef_group, attribute, group, entry) %>%
  spread(key = group, value = entry) %>%
  select(-coef_group)

all_df$group<- factor(all_df$group, levels = c("Male", "Female", "Difference"))

all_df[43,9] = "Base category: 0 - 5"
all_df[44,9] = "Base category: 0 - 5"


g3 <-
  ggplot(all_df, aes(x = Estimate, y = attribute)) +
  geom_point() +
  geom_segment(aes(yend = attribute, x = lis, xend = uis)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  facet_grid(coef_group ~ group, scales = "free_y") +
  coord_cartesian(xlim = c(-.4, .4)) +
  scale_x_continuous(breaks = round(seq(-.3, .3, .1), 1)) +
  theme_bw() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    strip.background = element_blank()
  )
g3

################################### DLABSS 2 Survey ###################################
#######################################################################################

## May be a good idea to start a new session to run code below because of similar functions from earlier

## FIGURE A16

# Replication Archive for: 
# Kirkland, Patricia A. and Alexander Coppock
# Candidate Choice without Party Labels: New Insights from Conjoint Survey Experiments
# Forthcoming at Political Behavior
# Helper Functions

#### Adapted for DLABSS 2 data

gen_entry <- function(est, se, p){
  entry <- paste0(format_num(est, digits = 2), " (", format_num(se, 2), ")")
  
  if(p < 0.05) {
    entry <- paste0(entry, "*")
  }
  return(entry)
}

gen_entry_vec <- Vectorize(gen_entry)

se_mean <- function(x){
  x_nona <- x[!is.na(x)]
  n <- length(x_nona)
  return(sd(x_nona)/(sqrt(n)))
}

format_num <- function(x, digits=3){
  x <- as.numeric(x)
  return(paste0(sprintf(paste0("%.", digits, "f"), x)))
}

make_attributes <- function(df){
  df <- within(df,{
               attribute = factor(attribute, 
                                  levels = c("Yes", "Base category: No", 
                                  
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator", "Hard-Working", "Tough Negotiator",
                                             "Base category: Empathetic",
                                             
                                             "City council member", "Mayor", "Representative in congress",
                                             "Base category: None",
                                             
                                             "Educator", "Small business owner",
                                             "Base category: Attorney",
                                             
                                             "45", "55", "65",
                                             "Base category: 35",                                            						                                            
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
               })
  return(df)
}

make_coef_group <- function(df){
  df <- within(df,{
    coef_group <- rep(NA, nrow(df))
    coef_group[grepl(pattern="Progressive", x = rowname)] <- "Progressive"
    coef_group[grepl(pattern="Personalistic", x = rowname)] <- "Personalistic"
    coef_group[grepl(pattern="Experience", x = rowname)] <- "Experience"
    coef_group[grepl(pattern="Agenda", x = rowname)] <- "Agenda"
    coef_group[grepl(pattern="Job", x = rowname)] <- "Job"
    coef_group[grepl(pattern="Age", x = rowname)] <- "Age"
    coef_group <- factor(coef_group, levels=c("Progressive", "Personalistic", "Experience", 
                                              "Agenda", "Job", "Age"))  
  })
  return(df)
}


prep_for_gg <- function(fit_cl, WP=FALSE){
  df <- data.frame(ests =fit_cl[,1], ses = fit_cl[,2]) %>%
    add_rownames()
  df <- filter(df, grepl(pattern = "Progressive|Personalistic|Experience|Agenda|Job|Age", x = df$rowname))
  df <- within(df,{
    uis <- ests + 1.96*ses
    lis <- ests - 1.96*ses
    attribute <- sub(pattern = "Progressive|Personalistic|Experience|Agenda|Job|Age", replacement = "", x = df$rowname)
    attribute <- factor(attribute, levels = c("Yes", "Base category: No", 
                                  
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator", "Hard-Working", "Tough Negotiator",
                                             "Base category: Empathetic",
                                             
                                             "City council member", "Mayor", "Representative in congress",
                                             "Base category: None",
                                             
                                             "Educator", "Small business owner",
                                             "Base category: Attorney",
                                             
                                             "45", "55", "65",
                                             "Base category: 35",                                            						                                            
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
    if(WP){
      attribute <- sub(pattern = "Progressive|Personalistic|Experience|Agenda|Job|Age", replacement = "", x = df$rowname)
      attribute <- factor(attribute, levels = c("Yes", "Base category: No", 
                                  
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator", "Hard-Working", "Tough Negotiator",
                                             "Base category: Empathetic",
                                             
                                             "City council member", "Mayor", "Representative in congress",
                                             "Base category: None",
                                             
                                             "Educator", "Small business owner",
                                             "Base category: Attorney",
                                             
                                             "45", "55", "65",
                                             "Base category: 35",                                            						                                            
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
    }
  })
  return(df)
}


cl   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) }

cl_vcov   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  return(vcovCL)}

type_base_categories_dlabss_second = read.csv("type_base_categories_dlabss_second.csv", stringsAsFactors=F)

## feed in data

mydata2 = read.csv("dlabss_second.csv")
dim(mydata2)

newdata <- mydata2

## make sure variables are factors

newdata$Age <- as.factor(newdata$Age)

## relevel to specified baselines

newdata$Personalistic  <- relevel(newdata$Personalistic, "Empathetic")
newdata$Agenda  <- relevel(newdata$Agenda, "Very Few Changes")
newdata$Experience  <- relevel(newdata$Experience, "None")
newdata$Job <- relevel(newdata$Job, "Attorney")

## code below computes and plots AMCEs

fit_1 <-
  lm(
    candidate_vote ~ Progressive + Personalistic + Experience + Agenda + Job + Age,
    data = filter(newdata, Gender == "Male")
  )
fit_2 <-
  lm(
    candidate_vote ~ Progressive + Personalistic + Experience + Agenda + Job + Age,
    data = filter(newdata, Gender == "Female")
  )
fit_3 <-
  lm(
    candidate_vote ~ (Progressive + Personalistic + Experience + Agenda + Job + Age) * (Gender == "Female"),
    data =newdata
  )


fit_1_cl <- cl(
  dat = filter(newdata, Gender == "Male"),
  cluster = filter(newdata, Gender == "Male")$id,
  fm = fit_1
)
fit_2_cl <- cl(
  dat = filter(newdata, Gender == "Female"),
  cluster = filter(newdata, Gender == "Female")$id,
  fm = fit_2
)
fit_3_cl <- cl(dat = newdata,
               cluster = newdata$id,
               fm = fit_3)

fit_1_df <-
  data.frame(fit_1_cl[, ], group = "Male") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")
fit_2_df <-
  data.frame(fit_2_cl[, ], group = "Female") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")
fit_3_df <-
  data.frame(fit_3_cl[, ], group = "Difference") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")

## keep only interactions

fit_3_df <- fit_3_df[19:35, ]
fit_3_df$rowname <- fit_1_df$rowname


all_df <- rbind(fit_1_df, fit_2_df, fit_3_df) %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    uis = Estimate + 1.96 * Std..Error,
    attribute = sub(
      pattern = "Progressive|Personalistic|Experience|Agenda|Job|Age",
      replacement = "",
      x = rowname
    )
  ) %>%
  bind_rows(type_base_categories_dlabss_second) %>%
  make_attributes() %>%
  make_coef_group()

table_d4 <- 
all_df %>%
  filter(!is.na(Estimate)) %>%
  mutate(entry = gen_entry_vec(est = Estimate, se = Std..Error, p = Pr...t..))  %>%
  select(coef_group, attribute, group, entry) %>%
  spread(key = group, value = entry) %>%
  select(-coef_group)

all_df$group<- factor(all_df$group, levels = c("Male", "Female", "Difference"))

all_df[11,10] = "Agenda"
all_df[12,10] = "Agenda"

all_df[28,10] = "Agenda"
all_df[29,10] = "Agenda"

all_df[45,10] = "Agenda"
all_df[46,10] = "Agenda"

all_df[61,10] = "Agenda"
all_df[62,10] = "Agenda"
all_df[63,10] = "Agenda"


g3 <-
  ggplot(all_df, aes(x = Estimate, y = attribute)) +
  geom_point() +
  geom_segment(aes(yend = attribute, x = lis, xend = uis)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  facet_grid(coef_group ~ group, scales = "free_y") +
  coord_cartesian(xlim = c(-.4, .4)) +
  scale_x_continuous(breaks = round(seq(-.3, .3, .1), 1)) +
  theme_bw() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    strip.background = element_blank()
  )
g3

################################### Prolific Survey ###################################
#######################################################################################

## May be a good idea to start a new session to run code below because of similar functions from earlier

## FIGURE A17

# Replication Archive for: 
# Kirkland, Patricia A. and Alexander Coppock
# Candidate Choice without Party Labels: New Insights from Conjoint Survey Experiments
# Forthcoming at Political Behavior
# Helper Functions

#### Adapted for Prolific data

gen_entry <- function(est, se, p){
  entry <- paste0(format_num(est, digits = 2), " (", format_num(se, 2), ")")
  
  if(p < 0.05) {
    entry <- paste0(entry, "*")
  }
  return(entry)
}

gen_entry_vec <- Vectorize(gen_entry)

se_mean <- function(x){
  x_nona <- x[!is.na(x)]
  n <- length(x_nona)
  return(sd(x_nona)/(sqrt(n)))
}

format_num <- function(x, digits=3){
  x <- as.numeric(x)
  return(paste0(sprintf(paste0("%.", digits, "f"), x)))
}

make_attributes2 <- function(df){
  df <- within(df,{
               attribute = factor(attribute, 
                                  levels = c("Yes", "Base category: No", 
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator",
                                             "Hard-Working", 
                                             "Tough Negotiator", 
                                             "Base category: Empathetic", 
                                             "3",
                                             
                                             "2", 
                                             "1", 
                                             "Base category: 0",
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
               })
  return(df)
}

make_coef_group2 <- function(df){
  df <- within(df,{
    coef_group <- rep(NA, nrow(df))
    coef_group[grepl(pattern="Progressive", x = rowname)] <- "Progressive"
    coef_group[grepl(pattern="Personalistic", x = rowname)] <- "Personalistic"
    coef_group[grepl(pattern="Parental", x = rowname)] <- "Parental"
    coef_group[grepl(pattern="Agenda", x = rowname)] <- "Agenda"
    coef_group <- factor(coef_group, levels=c("Progressive", "Personalistic", "Parental", 
                                              "Agenda"))  
  })
  return(df)
}


prep_for_gg2 <- function(fit_cl, WP=FALSE){
  df <- data.frame(ests =fit_cl[,1], ses = fit_cl[,2]) %>%
    add_rownames()
  df <- filter(df, grepl(pattern = "Progressive|Personalistic|Parental|Agenda", x = df$rowname))
  df <- within(df,{
    uis <- ests + 1.96*ses
    lis <- ests - 1.96*ses
    attribute <- sub(pattern = "Progressive|Personalistic|Parental|Agenda", replacement = "", x = df$rowname)
    attribute <- factor(attribute, levels = c("Yes", "Base category: No", 
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator",
                                             "Hard-Working", 
                                             "Tough Negotiator", 
                                             "Base category: Empathetic", 
                                             "3",
                                             
                                             "2", 
                                             "1", 
                                             "Base category: 0",
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
    if(WP){
      attribute <- sub(pattern = "Progressive|Personalistic|Parental|Agenda", replacement = "", x = df$rowname)
      attribute <- factor(attribute, levels = c("Yes", "Base category: No", 
                                             "Assertive", "Collaborative", "Determined to Succeed", 
                                             "Good Communicator",
                                             "Hard-Working", 
                                             "Tough Negotiator", 
                                             "Base category: Empathetic", 
                                             "3",
                                             
                                             "2", 
                                             "1", 
                                             "Base category: 0",
                                             "Complete Overhaul", 
                                             "Moderate Changes", 
                                             "Base category: Very Few Changes"))
    }
  })
  return(df)
}


cl   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  coeftest(fm, vcovCL) }

cl_vcov   <- function(dat,fm, cluster){
  require(sandwich, quietly = TRUE)
  require(lmtest, quietly = TRUE)
  M <- length(unique(cluster))
  N <- length(cluster)
  K <- fm$rank
  dfc <- (M/(M-1))*((N-1)/(N-K))
  uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
  vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
  return(vcovCL)}

type_base_categories_prolific = read.csv("type_base_categories_prolific.csv", stringsAsFactors=F)

## feed in data

mydata2 = read.csv("prolific.csv")
dim(mydata2)

newdata <- mydata2

## make sure variables are factors

newdata$Parental <- as.factor(newdata$Parental)

## relevel to specified baselines

newdata$Personalistic  <- relevel(newdata$Personalistic, "Empathetic")
newdata$Agenda  <- relevel(newdata$Agenda, "Very Few Changes")

## code below computes and plots AMCEs

fit_1 <-
  lm(
    candidate_vote ~ Progressive + Personalistic + Parental + Agenda,
    data = filter(newdata, Gender == "Male")
  )
fit_2 <-
  lm(
    candidate_vote ~ Progressive + Personalistic + Parental + Agenda,
    data = filter(newdata, Gender == "Female")
  )
fit_3 <-
  lm(
    candidate_vote ~ (Progressive + Personalistic + Parental + Agenda) *  (Gender == "Female") ,
    data = newdata
  )


fit_1_cl <- cl(
  dat = filter(newdata, Gender == "Male"),
  cluster = filter(newdata, Gender == "Male")$id,
  fm = fit_1
)
fit_2_cl <- cl(
  dat = filter(newdata, Gender == "Female"),
  cluster = filter(newdata, Gender == "Female")$id,
  fm = fit_2
)
fit_3_cl <- cl(dat = newdata,
               cluster = newdata$id,
               fm = fit_3)

fit_1_df <-
  data.frame(fit_1_cl[, ], group = "Male") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")
fit_2_df <-
  data.frame(fit_2_cl[, ], group = "Female") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")
fit_3_df <-
  data.frame(fit_3_cl[, ], group = "Difference") %>% rownames_to_column() %>%
  filter(rowname != "(Intercept)")

##take just the interactions 
fit_3_df <- fit_3_df[14:25, ]
fit_3_df$rowname <- fit_1_df$rowname
fit_3_df


all_df <- rbind(fit_1_df, fit_2_df, fit_3_df) %>%
  mutate(
    lis = Estimate - 1.96 * Std..Error,
    uis = Estimate + 1.96 * Std..Error,
    attribute = sub(
      pattern = "Progressive|Personalistic|Parental|Agenda",
      replacement = "",
      x = rowname
    )
  ) %>%
  bind_rows(type_base_categories_prolific) %>%
  make_attributes2() %>%
  make_coef_group2()


table_d4 <- 
all_df %>%
  filter(!is.na(Estimate)) %>%
  mutate(entry = gen_entry_vec(est = Estimate, se = Std..Error, p = Pr...t..))  %>%
  select(coef_group, attribute, group, entry) %>%
  spread(key = group, value = entry) %>%
  select(-coef_group)

all_df$group<- factor(all_df$group, levels = c("Male", "Female", "Difference"))


g3 <-
  ggplot(all_df, aes(x = Estimate, y = attribute)) +
  geom_point() +
  geom_segment(aes(yend = attribute, x = lis, xend = uis)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  facet_grid(coef_group ~ group, scales = "free_y") +
  coord_cartesian(xlim = c(-.5, .5)) +
  scale_x_continuous(breaks = round(seq(-.5, .5, .1), 1)) +
  theme_bw() +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    strip.background = element_blank()
  )

g3




